package com.example.mapper;

import com.example.entity.Course;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface CourseMapper {

    @Insert("insert into course(name, content, score, teacher_id, num, time, location, college_id, already_num) " +
            "values(#{name}, #{content}, #{score}, #{teacherId}, #{num}, #{time}, #{location}, #{collegeId}, #{alreadyNum})")
    void add(Course course);

    @Select("SELECT c.*,t.`name` teacherName,g.`name` collegeName FROM course c " +
            "LEFT JOIN teacher t ON c.teacher_id=t.id LEFT JOIN college g ON c.college_id=g.id " +
            "WHERE c.teacher_id=#{teacherId} AND c.`name` LIKE CONCAT('%',#{name},'%')")
    List<Course> selectByNameAndTeacherId(String name, Integer teacherId);

    @Select("SELECT c.*,t.`name` teacherName,g.`name` collegeName FROM course c " +
            "LEFT JOIN teacher t ON c.teacher_id=t.id LEFT JOIN college g ON c.college_id=g.id " +
            "WHERE c.`name` LIKE CONCAT('%',#{name},'%')")
    List<Course> selectByName(String name);

    @Select("SELECT c.*,t.`name` teacherName,g.`name` collegeName FROM course c " +
            "LEFT JOIN teacher t ON c.teacher_id=t.id LEFT JOIN college g ON c.college_id=g.id " +
            "WHERE c.teacher_id=#{teacherId}")
    List<Course> selectByTeacherId(Integer teacherId);

    @Select("SELECT c.*,t.`name` teacherName,g.`name` collegeName FROM course c " +
            "LEFT JOIN teacher t ON c.teacher_id=t.id LEFT JOIN college g ON c.college_id=g.id ")
    List<Course> selectAll();

    @Update("update course set name = #{name}, content = #{content},score = #{score}," +
            "teacher_id = #{teacherId}, num = #{num},time = #{time},location = #{location}," +
            "college_id = #{collegeId}, already_num = #{alreadyNum} where id = #{id}")
    void updateById(Course course);

    @Delete("DELETE FROM course WHERE id=#{id}")
    void deleteById(Integer id);

    @Select("select * from  course WHERE id=#{id}")
    Course selectById(Integer courseId);
}
